package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.atguigu.srb.result.ResponseEnum;
import com.atguigu.srb.result.ResponseVo;
import com.atguigu.srb.util.SrbAssert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2024-04-15
 */
// 管理员访问的积分等级接口
@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "管理员的积分等级接口")
@Slf4j
//@CrossOrigin
public class AdminIntegralGradeControllerAdmin {
    @Autowired
    private IntegralGradeService integralGradeService;
    @ApiOperation("获取所有的积分等级")
    @GetMapping
    public ResponseVo list(){
//        System.out.println(1/0);  // 模拟异常
//        log.error("我是error级别的日志");
//        log.warn("我是warning级别的日志");
//        log.info("我是info级别的日志");
//        log.debug("我是debug级别的日志");
        return  ResponseVo.ok().addData("items",integralGradeService.list());
    }

    @ApiOperation("根据id查询积分等级")
    @GetMapping("/{id}")
    public ResponseVo getById(@PathVariable Long id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if(integralGrade == null){
            return ResponseVo.setResult(ResponseEnum.NOT_FOUND);
        }
        return ResponseVo.ok().addData("item",integralGrade);
    }

    @ApiOperation("添加积分等级")
    @PostMapping
    public ResponseVo save(@RequestBody IntegralGrade integralGrade){
        boolean save = integralGradeService.save(integralGrade);
        return save?ResponseVo.ok():ResponseVo.error().message("添加积分等级失败");
    }

    @ApiOperation("根据id删除积分等级")
    @DeleteMapping("{id}")
    public ResponseVo removeById(@PathVariable Long id){
        boolean b = integralGradeService.removeById(id);
        return  b?ResponseVo.ok().message("删除成功"):ResponseVo.error().message("删除失败");
    }

    @ApiOperation("根据id修改积分等级")
    @PutMapping
    public ResponseVo updateById(@RequestBody IntegralGrade integralGrade){
        SrbAssert.assertNotNull(integralGrade.getId());
        boolean b = integralGradeService.updateById(integralGrade);
        return b?ResponseVo.ok().message("更新"+integralGrade.getId()+"成功") :
                ResponseVo.error().message("更新"+integralGrade.getId()+"失败");
    }
}

